﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

public partial class backstage_send_message : System.Web.UI.Page
{
    // Fiel
    protected InBoxManager ibm = new InBoxManager();
    protected OutBox ob = new OutBox();
    protected OutBoxManager obm = new OutBoxManager();

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.ob.Sender = "管理员";
            this.ob.Addressee = this.txtAddressee.Value;
            this.ob.Title = this.txtTitle.Value;
            this.ob.Content = this.txtaContent.Value;
            this.ob.LanguageVersion = this.Session["language_version"].ToString();
            if (this.obm.SendEmail(this.ob))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送成功！\");location.href=\"out_box.aspx\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送失败！\");</script>");
            }
        }
        catch
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送失败！\");</script>");
        }
    }

    public StringBuilder GetBcakLocation()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if (base.Request.QueryString["t"] == null)
            {
                return builder;
            }
            if (base.Request.QueryString["t"] == "ib")
            {
                builder.Append("location.href='in_box.aspx';");
                return builder;
            }
            builder.Append("location.href='out_box.aspx';");
        }
        catch
        {
        }
        return builder;
    }

    public StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if ((base.Request.QueryString["ir"] != null) && (base.Request.QueryString["id"] != null))
            {
                builder.Append("回复消息");
                return builder;
            }
            builder.Append("发送消息");
        }
        catch
        {
        }
        return builder;
    }

    private void OutBoxBind()
    {
        try
        {
            if (Convert.ToInt32(base.Request.QueryString["ir"]) == 0)
            {
                this.ibm.ModifyInBoxReadById(Convert.ToInt32(base.Request.QueryString["id"]));
            }
            DataTable inBoxById = this.ibm.GetInBoxById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (inBoxById.Rows.Count > 0)
            {
                this.txtAddressee.Value = inBoxById.Rows[0]["sender"].ToString();
                this.txtAddressee.Attributes.Add("readonly", "readonly");
                this.txtTitle.Value = inBoxById.Rows[0]["title"].ToString();
                this.txtaContent.Value = inBoxById.Rows[0]["content"].ToString();
            }
        }
        catch
        {
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      AjaxPro.Utility.RegisterTypeForAjax(typeof(UserManager));
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack && ((base.Request.QueryString["ir"] != null) && (base.Request.QueryString["id"] != null)))
        {
            this.OutBoxBind();
        }
    }

}